Micron Document
Fox's Git Mirrors

Node / acehoss / PyTCP.git / files / pytcp / protocols / udp / metadata.py

Displaying Raw • Download

pytcp/protocols/udp/metadata.py c0d45889ebbae797865487313d1e1efc691032f5 (c0d45889) Text, 3.61 KB

#!/usr/bin/env python3

############################################################################
# #
# PyTCP - Python TCP/IP stack #
# Copyright (C) 2020-2021 Sebastian Majewski #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# Author's email: ccie18643@gmail.com #
# Github repository: https://github.com/ccie18643/PyTCP #
# #
############################################################################


#
# protocols/udp/metadata.py - module contains interface class for FPP -> UDP Socket communication
#


from __future__ import annotations

from typing import TYPE_CHECKING

from lib.ip4_address import Ip4Address

if TYPE_CHECKING:
from lib.ip_address import IpAddress
from lib.tracker import Tracker


class UdpMetadata:
"""Store AF_INET6/SOCK_DGRAM metadata"""

def __init__(
self,
local_ip_address: IpAddress,
local_port: int,
remote_ip_address: IpAddress,
remote_port: int,
data: bytes = b"",
tracker: Tracker | None = None,
) -> None:
self.local_ip_address = local_ip_address
self.local_port = local_port
self.remote_ip_address = remote_ip_address
self.remote_port = remote_port
self.data = data
self.tracker = tracker

def __str__(self) -> str:
"""String representation"""

return f"AF_INET{self.local_ip_address.version}/SOCK_DGRAM/{self.local_ip_address}/{self.local_port}/{self.remote_ip_address}/{self.remote_port}"

@property
def socket_patterns(self) -> list[str]:
"""Socket ID patterns that match this packet"""

patterns = [
f"AF_INET{self.local_ip_address.version}/SOCK_DGRAM/{self.local_ip_address}/{self.local_port}/{self.remote_ip_address}/{self.remote_port}",
f"AF_INET{self.local_ip_address.version}/SOCK_DGRAM/{self.local_ip_address}/{self.local_port}/{self.local_ip_address.unspecified}/0",
f"AF_INET{self.local_ip_address.version}/SOCK_DGRAM/{self.local_ip_address.unspecified}/{self.local_port}/{self.local_ip_address.unspecified}/0",
]

if isinstance(self.local_ip_address, Ip4Address):
patterns.append(f"AF_INET4/SOCK_DGRAM/0.0.0.0/{self.local_port}/255.255.255.255/{self.remote_port}") # For DHCPv4 client

return patterns

Served by rngit 1.5.2 - Generated in 0.02s